package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.OrderDetail;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.OrdersMapper;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderDetailService;
import com.itheima.reggie.service.OrdersService;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private OrderDetailService orderDetailService;


    @Override
    public void saveWithDetail(Orders orders) {

        Long currentId = BaseContext.getCurrentId();

        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AddressBook::getUserId,currentId);
        wrapper.eq(AddressBook::getId,orders.getAddressBookId());
        AddressBook addressBook = addressBookService.getOne(wrapper);
        OrdersDto ordersDto = new OrdersDto();


        //设置订单状态
        //订单状态 1待付款，2待派送，3已派送，4已完成，5已取消
        orders.setStatus(2);

        orders.setUserId(currentId);

        //设置点击支付下单时间
        orders.setCheckoutTime(LocalDateTime.now());


        QueryWrapper<ShoppingCart> wrapper1 = new QueryWrapper<>();
        wrapper1.select("sum(amount*number) as 'total',user_id")
                .eq("user_id",currentId)
                .groupBy("user_id");
        Map<String, Object> map = shoppingCartService.getMap(wrapper1);
        BigDecimal total =(BigDecimal) map.get("total");
        orders.setAmount(total);
        orders.setOrderTime(LocalDateTime.now());
//        this.save(orders);

        BeanUtils.copyProperties(orders,ordersDto);

        //设置地址
        ordersDto.setAddress(addressBook.getDetail());

        //电话
        ordersDto.setPhone(addressBook.getPhone());

        //设置收货人
        ordersDto.setConsignee(addressBook.getConsignee());

        this.save(ordersDto);


        LambdaQueryWrapper<ShoppingCart> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ShoppingCart::getUserId,currentId);
        List<ShoppingCart> list = shoppingCartService.list(wrapper2);
        List<OrderDetail> detailList = list.stream().map(item->{
            OrderDetail detail = new OrderDetail();
            detail.setOrderId(ordersDto.getId());
            detail.setNumber(item.getNumber());
            detail.setAmount(item.getAmount());
            detail.setName(item.getName());
            detail.setImage(item.getImage());
            Long dishId = item.getDishId();
            if(dishId!=null){
                detail.setDishId(dishId);
            }else{
                detail.setSetmealId(item.getSetmealId());
            }
            detail.setDishFlavor(item.getDishFlavor());
            return detail;
        }).collect(Collectors.toList());

        orderDetailService.saveBatch(detailList);

//        //下单后删除购物车数据
//        LambdaQueryWrapper<ShoppingCart> wrapper3 = new LambdaQueryWrapper<>();
//        wrapper3.eq(ShoppingCart::getUserId,currentId);
//        shoppingCartService.remove(wrapper3);


    }
}
